﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PMS
{
    public partial class frmAddEmployee : Form
    {
        DBOperate operate = new DBOperate();
        public frmAddEmployee()
        {
            InitializeComponent();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Close();                                                   //关闭窗体
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                operate.Read_Image(openFileDialog1, pictureBox1);          //加载员工头像
            }
            catch                                                          //捕获异常
            {
                MessageBox.Show("如果加载图片出错");                       //弹出消息对话框
            }
        }

        private void frmAddEmployee_Load(object sender, EventArgs e)
        {
            try
            {
                operate.BindDropdownlist("tb_department",cbbYGBumen,1);
                cbbYGHunyin.SelectedIndex = 0;
                cbbYGSex.SelectedIndex = 0;
                cbbYGBumen.SelectedIndex = 0;
                txtYGminzu.SelectedIndex = 0;
                string strg = Application.StartupPath.ToString();
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg += @"\PMSImage";
                strg += @"\default.jpg";
                openFileDialog1.FileName = strg;                                //设置打开文件路径信息
                operate.BindDropdownlist("tb_userJob", txtYGminzu, 1);          //绑定所有得职务列表
            }
            catch(Exception ex)                                                 //捕获异常
            {
                MessageBox.Show(ex.Message);                                    //弹出消息对话框
            }
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            if(txtYGBirthday.Text.Trim() == ""|| txtYGJiuzhi.Text.Trim() == ""|| txtYGminzu.Text.Trim() == ""||
                txtYGName.Text.Trim() == ""|| txtYGNum.Text.Trim() == ""|| 
                txtYGPhone.Text.Trim() == ""|| txtYGZhiwu.Text.Trim() == ""|| txtYGPay.Text.Trim() == "")
            {
                MessageBox.Show("请将信息填写完整", "警告", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);        //弹出消息对话框
                return;                                                       //退出事件
            }
            else
            {
                if(txtYGPhone.TextLength!= 11)
                {
                    MessageBox.Show("手机号码为11位");                         //弹出消息对话框
                    return;
                }
                else
                {
                    string str =
                    //创建SQL字符串
                        "select count(*) from tb_employee where employeeID='" + txtYGNum.Text + "'";
                    //得到记录数量
                    int i = operate.HumanNum(str);                                    
                    if(i > 0)
                    {
                        MessageBox.Show("该员工编号已经存在", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        string strSql =
                            //创建SQL字符串
                            "insert into tb_employee(employeeID,employeeName,employeeSex,employeeDept,employeeBirthday,employeeNation,employeeMarriage,employeeDuty,employeePhone,employeeAccession,employeePay) values('" + txtYGNum.Text.Trim() + "','" + txtYGName.Text.Trim() + "','" +
                            cbbYGHunyin.Text + "','" + txtYGZhiwu.Text.Trim() + "','" + txtYGPhone.Text.Trim() + "','" + txtYGJiuzhi.Text + "','" + txtYGPay.Text.Trim() + "')";
                        int num = operate.OperateData(strSql);
                        operate.SaveImage(this.txtYGNum.Text.Trim(), openFileDialog1);  
                        if(num > 0)
                        {
                            MessageBox.Show("员工信息添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }

        private void txtYGNum_TextChanged(object sender, EventArgs e)
        {
            string str =
                //创建SQL字符串
                "select count(*) from tb_employee where employeeID='" + this.txtYGNum.Text.Trim() + "'";
            int m = operate.HumanNum(str);                                      //得到记录数量
            if(m > 0)
            {
                MessageBox.Show("员工编号存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
    }
}
